/**
 * 
 */
package org.jenkon.collection;

import java.util.Collection;

import org.jenkon.decorator.CollectionDecorator;
import org.jenkon.filter.Filter;
import org.jenkon.filter.Filterable;
import org.jenkon.utils.ClassUtils;

/**
 * @author Jens Konopka
 * 
 * @param <E> the type
 */
public class FilterableCollection<E> extends CollectionDecorator<E> implements Filterable<Collection<E>, E> {

	/**
	 * @param decorator the {@link Collection} decorator
	 */
	public FilterableCollection(final Collection<E> decorator) {
		super(decorator);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.commons.lang.filter.Filterable#filter(org.jenkon.commons.lang.filter.Filter)
	 */
	public Collection<E> filter(final Filter<E> filter) {
		final Collection<E> results = ClassUtils.newInstance(decorator);
		for (final E value : decorator) {
			if (filter.accept(value)) {
				results.add(value);
			}
		}
		return results;
	}

	/**
	 * Test with the {@link Filter} of this contains specific element by {@link Filter}.
	 * 
	 * @param filter {@link Filter}
	 * @return <code>true</code> or <code>false</code>
	 * @throws NullPointerException if the {@link Filter} is <code>null</code>
	 * @see Filter
	 * @see #filter(Filter)
	 */
	public boolean contains(final Filter<E> filter) {
		for (final E value : decorator) {
			if (filter.accept(value)) {
				return true;
			}
		}
		return false;
	}

}
